package org.ms.mconf.server.springboot.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.ms.mconf.sdk.model.MetaConfig;
import org.ms.mconf.sdk.model.MetaDataConfig;
import org.ms.mconf.sdk.model.MetaMultiDataConfig;
import org.ms.mconf.server.springboot.BaseApi;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("config")
@Api(description = "The micro config service api")
public class ConfigApi extends BaseApi {

    @ApiOperation(value = "search", notes = "The get all config data list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "node", value = "The node id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "environment", value = "The environment id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "application", value = "The application id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "config", value = "The config id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "version", value = "The version id", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "The search keyword", dataType = "string", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public List<MetaDataConfig> search(
            @RequestParam(value = "node", required = false) String node,
            @RequestParam(value = "environment", required = false) String environment,
            @RequestParam(value = "application", required = false) String application,
            @RequestParam(value = "config", required = false) String config,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "keyword", required = false) String keyword) {
        return super.opsService().search(new MetaConfig(node, environment, application, config, version), keyword);
    }

    @ApiOperation(value = "publish", notes = "The publish config data")
    @ApiImplicitParam(name = "metaMultiDataConfig", value = "The meta multi data config data", required = true, dataType = "MetaMultiDataConfig", paramType = "body")
    @RequestMapping(method = RequestMethod.POST)
    public boolean publish(@RequestBody MetaMultiDataConfig metaMultiDataConfig) {
        return super.opsService().publish(metaMultiDataConfig);
    }

    @ApiOperation(value = "delete", notes = "The delete config data")
    @ApiImplicitParam(name = "metaConfig", value = "The meta config", required = true, dataType = "MetaConfig", paramType = "body")
    @RequestMapping(method = RequestMethod.DELETE)
    public boolean delete(@RequestBody MetaConfig metaConfig) {
        return super.opsService().delete(metaConfig);
    }

}
